package com.muhuahealth.controller;

import com.alibaba.druid.sql.PagerUtils;
import com.muhuahealth.entity.UndergroundPipeEntity;
import com.muhuahealth.service.UndergroundPipeService;
import com.muhuahealth.utils.PageQueryUtil;
import com.muhuahealth.utils.PageResult;
import com.muhuahealth.utils.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author GSK
 * @version 1.0
 * @date 2021/1/24 17:32
 */
@Slf4j
@Controller
@RequestMapping("/admin/pipe")
public class UndergroundPipeController {

    @Autowired
    UndergroundPipeService undergroundPipeService;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String undergroundPipePage(HttpServletRequest request){
        request.getSession().setAttribute("path", "Basic_data_pipe");
        log.info("跳转到地下管道页面");
        return "/Basic_data_pipe";
    }

    /**
     * 地下管道数据展示列表
     * @param params
     * @return
     */
    @GetMapping(value = "/list")
    @ResponseBody
    public ServerResponse pipeList(@RequestParam Map<String,Object> params){

        //判断分页查询是否正常，当前页码以及每页条数是否为空
        if(StringUtils.isEmpty(params.get("page")) || StringUtils.isEmpty(params.get("limit"))){
           return ServerResponse.createError("参数异常！");
        }

        //分页查询的参数page、limit
        PageQueryUtil queryUtil = new PageQueryUtil(params);
        //分页的工具类
        PageResult listPage = undergroundPipeService.getPipeListPage(queryUtil);
        return ServerResponse.createSuccess("success",listPage);
    }

    /**
     * 新增地下管道数据
     * @param undergroundPipeEntity
     * @return
     */
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse save(UndergroundPipeEntity undergroundPipeEntity){

        log.info("开始添加数据");
        if(StringUtils.isEmpty(undergroundPipeEntity.getTypes())
            || StringUtils.isEmpty(undergroundPipeEntity.getTowards())
            || StringUtils.isEmpty(undergroundPipeEntity.getPipeDiameter())
            || StringUtils.isEmpty(undergroundPipeEntity.getStartDeep())
            || StringUtils.isEmpty(undergroundPipeEntity.getEndDeep())
            || StringUtils.isEmpty(undergroundPipeEntity.getStartGroundElevation())
            || StringUtils.isEmpty(undergroundPipeEntity.getEndGroundElevation())
            || StringUtils.isEmpty(undergroundPipeEntity.getStartWellElevation())
            || StringUtils.isEmpty(undergroundPipeEntity.getEndWellElevation())
            || StringUtils.isEmpty(undergroundPipeEntity.getLengths())){
            return ServerResponse.createError("插入失败!");
        }
        return undergroundPipeService.savePipeInfo(undergroundPipeEntity);
    }

    /**
     * 更新地下管道数据
     * @param undergroundPipeEntity
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse update(UndergroundPipeEntity undergroundPipeEntity){

        log.info("开始更新数据");
        if(StringUtils.isEmpty(undergroundPipeEntity.getTypes())
                || StringUtils.isEmpty(undergroundPipeEntity.getTowards())
                || StringUtils.isEmpty(undergroundPipeEntity.getPipeDiameter())
                || StringUtils.isEmpty(undergroundPipeEntity.getStartDeep())
                || StringUtils.isEmpty(undergroundPipeEntity.getEndDeep())
                || StringUtils.isEmpty(undergroundPipeEntity.getStartGroundElevation())
                || StringUtils.isEmpty(undergroundPipeEntity.getEndGroundElevation())
                || StringUtils.isEmpty(undergroundPipeEntity.getStartWellElevation())
                || StringUtils.isEmpty(undergroundPipeEntity.getEndWellElevation())
                || StringUtils.isEmpty(undergroundPipeEntity.getLengths())){
            return ServerResponse.createError("更新失败!");
        }
        return undergroundPipeService.updatePipeInfo(undergroundPipeEntity);
    }

    /**
     * 删除地下管道数据
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ServerResponse delete(@RequestParam("id") Integer id){

        log.info("开始删除数据");
        if(undergroundPipeService.deleteById(id)){
            return ServerResponse.createSuccess();
        }
        return ServerResponse.createError("删除失败");
    }

    /**
     * 查询地下管道数据
     * @param undergroundPipeEntity
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse search(UndergroundPipeEntity undergroundPipeEntity){

        log.info("开始查询数据");
        if(StringUtils.isEmpty(undergroundPipeEntity.getTypes())
                || StringUtils.isEmpty(undergroundPipeEntity.getTowards())
                || StringUtils.isEmpty(undergroundPipeEntity.getPipeDiameter())
                || StringUtils.isEmpty(undergroundPipeEntity.getStartDeep())
                || StringUtils.isEmpty(undergroundPipeEntity.getEndDeep())
                || StringUtils.isEmpty(undergroundPipeEntity.getStartGroundElevation())
                || StringUtils.isEmpty(undergroundPipeEntity.getEndGroundElevation())
                || StringUtils.isEmpty(undergroundPipeEntity.getStartWellElevation())
                || StringUtils.isEmpty(undergroundPipeEntity.getEndWellElevation())
                || StringUtils.isEmpty(undergroundPipeEntity.getLengths())){
            return ServerResponse.createError("参数异常!");
        }
        return undergroundPipeService.searchPipeInfo(undergroundPipeEntity);
    }
}
